package com.eqdd.yiqidian.ui.yiqiyue;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewRefreshCustom;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.YueMenuArticleListBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.RefreshUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * @author吕志豪 .
 * @date 17-12-16  下午1:01.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class YueArticleListActivity extends BaseActivity {

    private RecyclerViewRefreshCustom dataBinding;
    private int menuId;
    private SlimAdapterEx<YueMenuArticleListBean> slimAdapterEx;

    @Override
    public void initBinding() {

        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview_refresh);
        menuId = getIntent().getIntExtra(Config.ID, 0);
        String title = getIntent().getStringExtra(Config.TITLE);
        initTopTitleBar(View.VISIBLE, title);

    }

    private void refresh() {
        OkGo.<HttpResult<List<YueMenuArticleListBean>>>post(HttpConfig.BASE_URL + HttpConfig.YUE_ARTICLE_LIST_BY_MENU)
                .params("userGuid", AppConstent.user.getGuid())
                .params("menuId", menuId)
                .execute(new DialogCallBack<HttpResult<List<YueMenuArticleListBean>>>(YueArticleListActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<YueMenuArticleListBean>>> response) {
                        HttpResult<List<YueMenuArticleListBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        dataBinding.coolRefreshView.setRefreshing(false);
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.updateData(httpResult.getItems());
                        }
                    }

                    @Override
                    public void onError(Response<HttpResult<List<YueMenuArticleListBean>>> response) {
                        super.onError(response);
                        dataBinding.coolRefreshView.setRefreshing(false);

                    }
                });
    }

    @Override
    public void initData() {
        RefreshUtil.openRefresh(dataBinding.coolRefreshView, coolRefreshView -> {
            refresh();
        });
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_45, new SlimInjector<YueMenuArticleListBean>() {
            @Override
            public void onInject(YueMenuArticleListBean data, IViewInjector injector) {
                injector.text(R.id.tv_upper_content, data.getTitle())
                        .text(R.id.tv_down_content, data.getCreaterTime());
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        dataBinding.recyclerView.setSwipeItemClickListener((view, i) -> {
            Bundle bundle = new Bundle();
            bundle.putInt(Config.ID, slimAdapterEx.getDataItem(i).getId());
            startActivity(ArticleInfoActivity.class, bundle);
        });
    }

    @Override
    public void setView() {

        dataBinding.coolRefreshView.setRefreshing(true);

    }
}
